package com.jcnet.bizaia;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;

import android.R.anim;
import android.R.integer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import com.baidu.cyberplayer.utils.as;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jcnet.bizaia.adapter.MainAdapter;
import com.jcnet.bizaia.bean.AllMainDetailHttpRequestBean;
import com.jcnet.bizaia.bean.CompanyInfoBean;
import com.jcnet.bizaia.bean.DisscussInfoBean;
import com.jcnet.bizaia.bean.TeacherInfoBean;
import com.jcnet.bizaia.fragment.ComperDetailFragment;
import com.jcnet.bizaia.fragment.DicussDetailFragment;
import com.jcnet.bizaia.fragment.TeacherDetailFragment;
import com.jcnet.bizaia.tools.Toolkit;
import com.jcnet.bizaia.view.ProgressHUD;
import com.jcnet.bizaia.xstream.ACache;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.squareup.picasso.Picasso;
import com.viewpagerindicator.IconPagerAdapter;
import com.viewpagerindicator.TabPageIndicator;

/**
 * @author chenguandong
 *
 */
/**
 * @author chenguandong
 *
 */
public class DiscussDetialActivity extends JCSimpleSherlockActivity {

	public static final int DISCUSS_ID = 1;
	public static final int VOD_ID = 2;
	public static final int VOICE_ID = 3;

	private TabPageIndicator indicator;

	private ViewPager viewPager;

	private String [] mainTitles; 

	private MainAdapter mainAdapter;

	private List<Fragment> fragments;


	private ProgressHUD progressHUD;
	
	
	private DisscussInfoBean chaChairJson = new DisscussInfoBean();	//讨论会详细
	
	private TeacherInfoBean chaLecturerJson = new TeacherInfoBean();    //讲师详细
	
	private CompanyInfoBean chaCompanyJson =new  CompanyInfoBean();;   //    公司详细
	
	private ACache aCache ;
	
	private String CACHE_HTTP = App.HTTP_PATH+"cha_showChairOne.action?";
	
	private GoogleMusicAdapter adapter;

	private String diss_imgUrlString ;
	
	private ImageView imageView;
	
	private int cridNum;
	
	private AsyncHttpClient asyncHttpClient;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		super.jcTitle = getString(R.string.disucess);
		super.jcSubtitle = getString(R.string.disucessDetail);
		super.initActionBar();
		
		
		asyncHttpClient = new AsyncHttpClient();
		
		setContentView(R.layout.discuss_detail);
		
		aCache = ACache.get(DiscussDetialActivity.this);
		
		diss_imgUrlString = getIntent().getStringExtra("imgUrl");
		
		imageView = (ImageView) findViewById(R.id.diss_img);
		
		cridNum = getIntent().getIntExtra("cridNum", -400);
		
		if(diss_imgUrlString!=null){
		Picasso.with(this).load(App.HTTP_PATH+diss_imgUrlString).fit().into(imageView);
		}
		
		mainTitles = getResources().getStringArray(R.array.discuss_sutitle_array);
		progressHUD = ProgressHUD.show(this, getString(R.string.dataloding), true, true, null);
		progressHUD.show();


		initView();

		
	}

	

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			break;

		default:
			break;
		}
		
		return super.onOptionsItemSelected(item);
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
		// TODO Auto-generated method stub
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		initAllData();
	}

	/**
	 *初始化页面的所有数据
	 */
	private void initAllData() {
		// TODO Auto-generated method stub
		
		final RequestParams params = new RequestParams();
		/**
		 * 	参数名称	参数类型	说明
			languageNum	int	语言种类：1：中文2：英文3：日文
			cridNum	int	讲座id
		 */
		params.put("languageNum", App.localeCode+"");
		params.put("cridNum", cridNum+"");
		
		if(Toolkit.isAvaliable(DiscussDetialActivity.this)){
		
			asyncHttpClient.post(App.HTTP_PATH+"cha_showChairOne.action?", params, new AsyncHttpResponseHandler(){
			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
			}
			
			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				// TODO Auto-generated method stub
				super.onSuccess(arg0, arg1, arg2);
				
				aCache.put(CACHE_HTTP+params.toString(), new String(arg2));
				
				initData(new String(arg2));
				
			}
			
			@Override
			public void onFinish() {
				// TODO Auto-generated method stub
				super.onFinish();
				if(progressHUD.isShowing()){
					progressHUD.cancel();
				}
			}
			
			
			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
				// TODO Auto-generated method stub
				super.onFailure(arg0, arg1, arg2, arg3);
				if(progressHUD.isShowing()){
					progressHUD.cancel();
				}
			}
		});
		}else{
			
			
			String cacheDataString =  aCache.getAsString(CACHE_HTTP+params);
			
			if(cacheDataString!=null){
			initData(new String(cacheDataString));
			}
			
			Toast.makeText(DiscussDetialActivity.this, getString(R.string.noNetWork), 0).show();
		}
		

	}

	private void initData(String jsonString){
		AllMainDetailHttpRequestBean allMainDetailHttpRequestBean =new Gson().fromJson(new String(jsonString), new TypeToken<AllMainDetailHttpRequestBean>(){}.getType());
		
		if(allMainDetailHttpRequestBean.getState()==200){
			chaCompanyJson =  allMainDetailHttpRequestBean.getChaCompanyJson();
			chaChairJson = allMainDetailHttpRequestBean.getChaChairJson();
			chaLecturerJson = allMainDetailHttpRequestBean.getChaLecturerJson();
			
			System.out.println("_________________________");
			System.out.println(chaChairJson.getTitle());
			System.out.println(chaChairJson.getDescribing());
			
			System.out.println("_________________________");
			
			fragments.clear();
			adapter.notifyDataSetChanged();
			fragments.add(DicussDetailFragment.newInstance(chaChairJson));
			fragments.add(TeacherDetailFragment.newInstance(chaLecturerJson));
			fragments.add(ComperDetailFragment.newInstance(chaCompanyJson));
			adapter.count = fragments.size();
			adapter.notifyDataSetChanged();
			
		}else {
			Toast.makeText(DiscussDetialActivity.this, allMainDetailHttpRequestBean.getMessage(), 0).show();
		}
	}
	

	private void initView() {
		// TODO Auto-generated method stub
		fragments = new ArrayList<Fragment>();
		fragments.add(DicussDetailFragment.newInstance(chaChairJson));
		fragments.add(TeacherDetailFragment.newInstance(chaLecturerJson));
		fragments.add(ComperDetailFragment.newInstance(chaCompanyJson));
		adapter  = new GoogleMusicAdapter(getSupportFragmentManager(),fragments);
		indicator = (TabPageIndicator)findViewById(R.id.indicator);
		viewPager = (ViewPager)findViewById(R.id.pager);
		viewPager.setAdapter(adapter);
		indicator.setViewPager(viewPager);
		
		indicator.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int arg0) {
				// TODO Auto-generated method stub
				if(arg0==2){
					imageView.setVisibility(View.GONE);
				}else{
					imageView.setVisibility(View.VISIBLE);
				}
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
				
			}
		});

	}



	class GoogleMusicAdapter extends FragmentStatePagerAdapter implements IconPagerAdapter {
		private int count;
		List<Fragment> fms;
		public GoogleMusicAdapter(FragmentManager fm ,List<Fragment> fms) {
			super(fm);
			this.fms = fms;
		
		}

		@Override
		 public int getItemPosition(Object object) {
		     return PagerAdapter.POSITION_NONE;
		 }
		@Override
		public Fragment getItem(int position) {

			return fms.get(position);
			//return TestFragment.newInstance(CONTENT[position % CONTENT.length]);
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return mainTitles[position];
		}

		@Override public int getIconResId(int index) {
			return 0;
		}

		@Override
		public int getCount() {
			count = fms.size();
			return count;
		}
	}

	


}
